package BookStore.dao;

import BookStore.model.Book_s;
import BookStore.util.BDbUtil;
import BookStore.view.U_Frame;

import javax.swing.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class Book_sDao {
    private Book_s book_s;

    public Book_sDao(Book_s temp_s) { this.book_s = temp_s; }

    public void sell() throws Exception {
        Connection con_3_4 = BDbUtil.getCon_3_4();
        String sql_s1 = "select * from VIP where vid = '" + book_s.getSellVid() + "' ";
        PreparedStatement ps = con_3_4.prepareStatement(sql_s1);
        ResultSet re = ps.executeQuery();
        String sql_s2 = "select * from BOOK where bid = '" + book_s.getSellBid() + "' ";
        ps = con_3_4.prepareStatement(sql_s2);
        ResultSet re1 = ps.executeQuery();
        String sql_s3 = "select * from BOOK where bid = '" + book_s.getSellBid() + "' and shuliang >= '" +  book_s.getSellShuliang() + "'";
        ps = con_3_4.prepareStatement(sql_s3);
        ResultSet re2 = ps.executeQuery();
        if (re.next()) {

            if (re1.next()) {

                if (re2.next()) {
                    String sql_s_1 = "insert into SELL (day,month,season,vid,bid,price,shuliang ) values ('" + book_s.getSellDay() + "','" + book_s.getSellMonth() + "','" + book_s.getSellSeason() + "','" +
                            book_s.getSellVid() + "','" + book_s.getSellBid() + "','" + book_s.getSellPrice() + "','" + book_s.getSellShuliang() + "')";
                    String sql_s_2 = "update BOOK set  shuliang = shuliang - '" + book_s.getSellShuliang() + "'where bid = '"+book_s.getSellBid()+"'";
                    PreparedStatement ps1 = con_3_4.prepareStatement(sql_s_1);
                    ResultSet res = ps1.executeQuery();
                    PreparedStatement ps2 = con_3_4.prepareStatement(sql_s_2);
                    ResultSet resu = ps2.executeQuery();
                    JOptionPane.showMessageDialog(null, "书籍销售成功");
                    new U_Frame().Uframe();
                    ps1.close();
                    ps2.close();

                }else{
                    JOptionPane.showMessageDialog(null, "该书库存不足");
                }
            }else{
                JOptionPane.showMessageDialog(null, "该书不存在");
            }

        } else {
            JOptionPane.showMessageDialog(null, "该会员不存在");
        }
        con_3_4.close();
        ps.close();
    }
}
